import {
  ref,
  reactive,
  toRefs,
  computed,
  getCurrentInstance,
  toRef,
} from "vue";
import regularMatch from "@/utils/regularMatch";

function useFeatureX() {
  // 响应式数据抽离出setup 具有代码可读性

  let checkMobile = (rule, value, callback) => {
    if (!value) {
      callback(new Error("请输入手机号"));
    } else if (!regularMatch.chinaMobileNumber(value)) {
      callback(new Error("请输入正确的手机号"));
    } else {
      callback();
    }
  };

  let validatePass = (rule, value, callback) => {
    if (!value) {
      callback(new Error("请输入密码"));
    } else if (!regularMatch.validatePass(value)) {
      callback(new Error("密码为6-20位数字母+数字组合"));
    } else {
      callback();
    }
  };

  const state = reactive({
    labelPosition: "right",
    hideRequiredAsterisk: true,
    rules_loginForm: {
      phoneNumber: [{ validator: checkMobile, trigger: "blur" }],
      password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        {
          min: 6,
          max: 20,
          message: "长度在 6 到 20 个字符",
          trigger: "blur",
        },
      ],
    },
    rules_registerForm: {
      phoneNumber: [{ validator: checkMobile, trigger: "blur" }],
      password: [
        // { required: true, message: "请输入密码", trigger: "blur" },
        // { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" },
        { validator: validatePass, trigger: "blur" },
      ],
    },
    rules_editPassForm: {
      password: [{ validator: validatePass, trigger: "blur" }],
      password2: [{ validator: validatePass, trigger: "blur" }],
    },
  });
  return state;
}

export default function() {
  const { emit } = getCurrentInstance();
  const mixin_loginData = useFeatureX();
  const clickBtn = () => emit("signUpModeChange");
  return { mixin_loginData, clickBtn };
}
